﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;


namespace NotebookS2
{
    public partial class FND_GLACCOUNT : RibbonForm
    {
        public FND_GLACCOUNT()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public string SelectedGLAcctCode { get; set; }
        public string SelectedGLAcctName { get; set; }


        void BindCOAByName()
        {
            ChartOfAccountManager oManager = new ChartOfAccountManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable dt = oManager.BrowseChartOfAccountByAcctName(txtSearch.Text);

            oManager.Close();

            gridControl1.DataSource = dt;
            gridControl1.Refresh();
        }

        void BindCOAByAccountCode()
        {
            ChartOfAccountManager oManager = new ChartOfAccountManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable dt = oManager.BrowseChartOfAccountByAcctCode(txtSearch.Text);

            oManager.Close();

            gridControl1.DataSource = dt;
            gridControl1.Refresh();
        }

        private void FND_GLACCOUNT_Load(object sender, EventArgs e)
        {
            comboBoxEdit1.SelectedIndex = 1;
        }

        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (comboBoxEdit1.SelectedIndex == 1)
            {
                BindCOAByName();
            }
            else
            {
                BindCOAByAccountCode();
            }
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            int iRowIndex = gridView1.FocusedRowHandle;

            this.SelectedGLAcctCode = gridView1.GetRowCellValue(iRowIndex, gridView1.Columns["AcctCode"]).ToString();
            this.SelectedGLAcctName = gridView1.GetRowCellValue(iRowIndex, gridView1.Columns["AcctName"]).ToString();

            this.Close();
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }

        }


        public void SetSearch(string sSearch)
        {
            txtSearch.Text = sSearch;

            if (comboBoxEdit1.SelectedIndex == 1)
            {
                BindCOAByName();
            }
            else
            {
                BindCOAByAccountCode();
            }            
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (comboBoxEdit1.SelectedIndex == 1)
                {
                    BindCOAByName();
                }
                else
                {
                    BindCOAByAccountCode();
                }
            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }
    }
}
